package com.ysy.tz.core.utils;

import com.ysy.tz.core.engine.KeyValuePair;
import com.ysy.tz.core.selfcomp.CustomerComboBox;
import com.ysy.tz.core.selfcomp.CustomerDatePicker;
import java.util.Date;
import java.util.Iterator;
import javafx.collections.ObservableList;
import javafx.scene.Node;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TextInputControl;
import javafx.scene.layout.Pane;
import org.apache.commons.lang3.StringUtils;

public class FormUtils {
    public FormUtils() {
    }

    public static <T> void setFormValues(Pane pane, T t) throws Exception {
        ObservableList<Node> nodeList = pane.getChildren();
        Iterator var3 = nodeList.iterator();

        while(true) {
            while(var3.hasNext()) {
                Node node = (Node)var3.next();
                if (node instanceof Pane) {
                    setFormValues((Pane)node, t);
                } else {
                    String id;
                    Object value;
                    if (node instanceof DatePicker) {
                        id = node.getId();
                        if (StringUtils.isNotBlank(id)) {
                            try {
                                value = t.getClass().getMethod("get" + StringUtils.capitalize(id)).invoke(t);
                                ((DatePicker)node).getEditor().setText(value.toString());
                            } catch (Exception var11) {
                            }
                        }
                    } else if (node instanceof TextInputControl) {
                        id = node.getId();
                        if (StringUtils.isNotBlank(id)) {
                            try {
                                value = t.getClass().getMethod("get" + StringUtils.capitalize(id)).invoke(t);
                                ((TextInputControl)node).setText(value.toString());
                            } catch (Exception var10) {
                            }
                        }
                    } else if (node instanceof CustomerComboBox) {
                        id = node.getId();
                        if (StringUtils.isNotBlank(id)) {
                            try {
                                value = t.getClass().getMethod("get" + StringUtils.capitalize(id)).invoke(t);
                                CustomerComboBox customerComboBox = (CustomerComboBox)node;
                                ObservableList<KeyValuePair> keyValuePairs = customerComboBox.getItems();

                                for(int i = 0; i < keyValuePairs.size(); ++i) {
                                    if (((KeyValuePair)keyValuePairs.get(i)).getKeyPair().equals(value)) {
                                        customerComboBox.getSelectionModel().select(i);
                                        break;
                                    }
                                }
                            } catch (Exception var12) {
                            }
                        }
                    }
                }
            }

            return;
        }
    }

    public static <T> T collectParams(Pane pane, Class<T> cls) throws Exception {
        T params = cls.newInstance();
        return createParamEntity(pane, params);
    }

    public static void resetFrom(Pane pane) {
        ObservableList<Node> nodeList = pane.getChildren();
        Iterator var2 = nodeList.iterator();

        while(var2.hasNext()) {
            Node node = (Node)var2.next();
            if (node instanceof Pane) {
                resetFrom((Pane)node);
            } else if (node instanceof DatePicker) {
                ((DatePicker)node).getEditor().setText(null);
                ((DatePicker)node).setValue(null);
            } else if (node instanceof TextInputControl) {
                ((TextInputControl)node).setText(null);
            } else if (node instanceof CustomerComboBox) {
                ((CustomerComboBox)node).setValue(null);
            }
        }

    }

    private static <T> T createParamEntity(Pane pane, T t) throws Exception {
        ObservableList<Node> nodeList = pane.getChildren();
        Iterator var3 = nodeList.iterator();

        while(var3.hasNext()) {
            Node node = (Node)var3.next();
            if (node instanceof Pane) {
                createParamEntity((Pane)node, t);
            } else {
                String id;
                String value;
                if (node instanceof DatePicker) {
                    id = node.getId();
                    if (StringUtils.isNotBlank(id)) {
                        value = ((DatePicker)node).getEditor().getText();
                        if (StringUtils.isNotBlank(value)) {
                            try {
                                t.getClass().getMethod("set" + StringUtils.capitalize(id), String.class).invoke(t, value);
                            } catch (Exception var10) {
                                t.getClass().getMethod("set" + StringUtils.capitalize(id), Date.class).invoke(t, ((CustomerDatePicker)node).getDate());
                            }
                        }
                    }
                } else if (node instanceof TextInputControl) {
                    id = node.getId();
                    if (StringUtils.isNotBlank(id)) {
                        value = ((TextInputControl)node).getText();
                        if (StringUtils.isNotBlank(value)) {
                            try {
                                t.getClass().getMethod("set" + StringUtils.capitalize(id), String.class).invoke(t, value);
                            } catch (Exception var9) {
                            }
                        }
                    }
                } else if (node instanceof CustomerComboBox) {
                    id = node.getId();
                    if (StringUtils.isNotBlank(id)) {
                        KeyValuePair keyValuePair = (KeyValuePair)((CustomerComboBox)node).getValue();
                        if (keyValuePair != null) {
                            try {
                                t.getClass().getMethod("set" + StringUtils.capitalize(id), String.class).invoke(t, keyValuePair.getKeyPair());
                            } catch (Exception var8) {
                            }
                        }
                    }
                }
            }
        }

        return t;
    }
}
